package com.yiyibook.controller;

import com.yiyibook.entity.Log;
import com.yiyibook.entity.Member;
import com.yiyibook.entity.Reg;
import com.yiyibook.entity.Upd;
import com.yiyibook.mapper.MemberMapper;
import com.yiyibook.service.MemberService;
import com.yiyibook.util.FieldErrorMsg;
import com.yiyibook.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 会员信息 Controller
 *
 * @author yi.wu
 * @date 2019-10-14
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberMapper memberMapper;

    @PostMapping("/register")
    public Msg register(@Validated({Reg.class}) Member member, BindingResult result){
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorMsg", FieldErrorMsg.errorMsg(result));
        } else {
            // 校验成功
            // TODO: 2019/10/14 查询会员账号是否重复
            Member m = memberService.selectByMemberName(member.getMemberName());
            if(m != null){
                return Msg.fail().add("errorMsg","会员账号重复");
            }
            int i = memberService.insert(member);
            if(i>0){
                return Msg.success().add("successMsg","注册成功");
            }else{
                return Msg.fail().add("errorMsg","网络波动，请重试");
            }
        }
    }

    @RequestMapping("/login")
    public Msg login(@Validated({Log.class}) @RequestBody Member member, BindingResult result){
        if (result.hasErrors()) {
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorFields", FieldErrorMsg.errorMsg(result));
        } else {
            // 校验成功
            return Msg.success().add("success",memberService.selectByCondition(member));
        }
    }

    @PatchMapping("update")
    public Msg update(@Validated({Upd.class}) @RequestBody Member member,BindingResult result){
        if(result.hasErrors()){
            //校验失败，显示校验失败的错误信息
            return Msg.fail().add("errorFields", FieldErrorMsg.errorMsg(result));
        }else {
            // 校验成功
            int i = memberService.updateByCondition(member);
            if(i>0){
                return Msg.success().add("success","修改成功！");
            }else {
                return Msg.fail().add("fail","网络波动，请重试！");
            }

        }

    }

    @GetMapping("/resetPwd")
    public Msg resetPassword(Integer memberId){
        Member member = new Member();
        member.setMemberId(memberId);
        member.setMemberPassword("1234qweR");
        int i = memberService.updateByCondition(member);
        if(i > 0){
            return Msg.success().add("successMsg","1234qweR");
        }else{
            return Msg.fail().add("errorMsg","网络波动，请重试");
        }
    }

    @GetMapping("/delete")
    public Msg deleteMember(Integer memberId){
        int i = memberService.deleteMemberById(memberId);
        if(i > 0){
            return Msg.success().add("successMsg","删除成功");
        }else{
            return Msg.fail().add("errorMsg","网络波动，请重试");
        }
    }

    @GetMapping("/deleteAny")
    public Msg deleteAnyMember(String memberIds){
        String[] split = memberIds.split(", ");
        Integer[] memberIdsArr = new Integer[split.length];
        for (int i = 0; i < memberIdsArr.length; i++) {
            memberIdsArr[i] = Integer.parseInt(split[i]);
        }
        int i = memberService.deleteAnyMemberByIds(Arrays.asList(memberIdsArr));
        if(i > 0){
            return Msg.success().add("successMsg","删除成功");
        }else{
            return Msg.fail().add("errorMsg","网络波动，请重试");
        }
    }

}
